package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import com.alibaba.fastjson.JSON;
import com.sinbader.xiaole.common.entity.Stock;
import com.sinbader.xiaole.common.entity.StockLog;
import com.sinbader.xiaole.common.mapper.StockLogMapper;
import com.sinbader.xiaole.common.model.params.StockLogParam;
import com.sinbader.xiaole.common.model.result.StockLogResult;
import  com.sinbader.xiaole.common.service.StockLogService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 库存变更记录 服务实现类
 */
@Service
public class StockLogServiceImpl extends ServiceImpl<StockLogMapper, StockLog> implements StockLogService {

    @Override
    public void add(StockLogParam param){
        StockLog entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StockLogParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StockLogParam param){
        StockLog oldEntity = getOldEntity(param);
        StockLog newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StockLogResult findBySpec(StockLogParam param){
        return null;
    }

    @Override
    public List<StockLogResult> findListBySpec(StockLogParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StockLogParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StockLogParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StockLog getOldEntity(StockLogParam param) {
        return this.getById(getKey(param));
    }

    private StockLog getEntity(StockLogParam param) {
        StockLog entity = new StockLog();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    public StockLog logByCompare(Stock oldStock, Stock newStock) {
    	StockLog stockLog = JSON.parseObject(JSON.toJSONString(newStock), StockLog.class);
		stockLog.setStockVersion(newStock.getVersion());

		stockLog.setSaleUsableChange(newStock.getSaleUsableCount() - oldStock.getSaleUsableCount());
		stockLog.setSaleFreezeChange(newStock.getSaleFreezeCount() - oldStock.getSaleFreezeCount());
		stockLog.setSaleUsedChange(newStock.getSaleUsedCount() - oldStock.getSaleUsedCount());

		stockLog.setWarehouseUsableChange(newStock.getWarehouseUsableCount() - oldStock.getWarehouseUsableCount());
		stockLog.setWarehouseFreezeChange(newStock.getWarehouseFreezeCount() - oldStock.getWarehouseFreezeCount());
		stockLog.setWarehouseUsedChange(newStock.getWarehouseUsedCount() - oldStock.getWarehouseUsedCount());

		save(stockLog);
    	return stockLog;
	}

}
